
/******************************************
 *            声音 控制
 *****************************************/
export default class qrj_audio {

    private static _buff_audio = [];

    /**
     *       加载 声音文件
     * @param audioName 
     */
    public static loadAudio(audioName: string) {
        if (qrj_audio._buff_audio[audioName] != null) {
            return;
        }
        cc.loader.loadRes("audio/" + audioName, function (err, au) {
            if (err) {
                console.error(err.message);
                return;
            }
            qrj_audio._buff_audio[audioName] = au;
            cc.log("加载音效文件:" + audioName);
        });
    }

    /**
     * 设置 缓冲区资源
     * @param audioName
     * @param res 
     */
    public static setAudio(audioName: string, res: any) {
        qrj_audio._buff_audio[audioName] = res;
    }

    /**
     *     声音  开关接口
     */
    private static _playAudio = true;
    public static set_playAudio(i: boolean) {
        qrj_audio._playAudio = i;
    }
    public static is_playAudio(): boolean {
        return qrj_audio._playAudio;
    }

    /**
     * 加载并播放 音效
     * @param audioName 
     * @param loop 
     * @param vol 
     */
    public static playAudio(audioName: string, loop = false, vol = 1) {
        if (qrj_audio.is_playAudio() == false) return;
        if (qrj_audio._buff_audio[audioName] != null) {
            cc.audioEngine.play(qrj_audio._buff_audio[audioName], loop, vol);
            return;
        }
        cc.loader.loadRes("audio/" + audioName, function (err, au) {
            if (err) {
                console.error(err.message);
                return;
            }

            qrj_audio._buff_audio[audioName] = au;
            cc.log("加载音效文件:" + audioName);
            cc.audioEngine.play(qrj_audio._buff_audio[audioName], loop, vol);
        });

        cc.log("play audio : " + audioName);
    }


    /**
     *     声音  开关接口
     */
    private static _playMusic = true;
    public static set_playMusic(i: boolean) {
        qrj_audio._playMusic = i;
    }
    public static is_playMusic(): boolean {
        return qrj_audio._playMusic;
    }

    private static music_id = null;
    private static music_name = null;
    /**
     *          播放音乐
     */
    public static playMusic = function (audioName: string) {
        if (qrj_audio.is_playMusic() == false) return;
        qrj_audio.stopMusic();

        cc.log("play music : " + audioName)
        qrj_audio.music_name = audioName;

        cc.loader.loadRes("audio/" + audioName, function (err, au) {
            if (err) {
                console.error(err.message);
                return;
            }
            qrj_audio.music_id = cc.audioEngine.play(au, true, 0.5);
        });
    }

    /**
     *      停止音乐
     */
    public static stopMusic() {
        if (qrj_audio.music_id != null) {
            cc.audioEngine.stop(qrj_audio.music_id);
            qrj_audio.music_id = null;
        }
    }


    /**
     *     暂停背景 音乐
     */
    public static pauseMusic() {
        if (qrj_audio.is_playMusic() == false) return;
        if (qrj_audio.music_id != null) {
            cc.audioEngine.stop(qrj_audio.music_id);
            qrj_audio.music_id = null;
        }
    }

    /**
     *     继续 背景音乐
     */
    public static resumeMusic() {
        if (qrj_audio.is_playMusic() == false) return;
        if (qrj_audio.music_name != null) {
            qrj_audio.playMusic(qrj_audio.music_name);
        }
    }

}
